﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;

namespace Net4.Common
{
    public static class WebUtil
    {
        /// <summary>
        /// Gets the upload directory for save the Attachment from Web.config.
        /// </summary>
        /// <value>The upload directory for save the Attachment.</value>
        public static string UploadDirectory
        {
            get
            {
                string dir = ConfigurationManager.AppSettings["UploadDirectory"];
                if (!dir.EndsWith("/"))
                {
                    dir = dir + "/";
                }
                return dir;
            }
        }

        /// <summary>
        /// Gets the relative root of the website.
        /// </summary>
        /// <value>A string that ends with a '/'.</value>
        public static string RelativeWebRoot
        {
            get { return VirtualPathUtility.ToAbsolute("~/"); }
        }

        private static Uri _AbsoluteWebRoot;

        /// <summary>
        /// Gets the absolute root of the website.
        /// </summary>
        /// <value>A string that ends with a '/'.</value>
        public static Uri AbsoluteWebRoot
        {
            get
            {
                if (_AbsoluteWebRoot == null)
                {
                    _AbsoluteWebRoot = new Uri(CurrentHttpContext.Request.Url.Scheme + "://"
                        + CurrentHttpContext.Request.Url.Authority + RelativeWebRoot);
                }
                return _AbsoluteWebRoot;
            }
        }

        public static HttpContext CurrentHttpContext
        {
            get
            {
                HttpContext context = HttpContext.Current;
                if (context == null)
                    throw new NullReferenceException("The current HttpContext is null");

                return context;
            }
        }
    }
}
